﻿using System;
using System.ComponentModel;
using System.Threading;
using System.Collections.Generic;
using CMN.Common;
using CMN.Entity;
using CMN.Models;
using CMN.Models.Interfaces;
using System.Text;
using System.IO;
namespace CMN.Domain
{
    public class AsyncPLInsert : AsyncBase
    {

        public string StrWhere = string.Empty;
        public AsyncPLInsert(ISynchronizeInvoke target)
            : base(target)
        {
           
        }
        //根据任务ID执行任务
        protected override void AsyncWork()
        {
            Update();                    
                
        }

        //已知任务步骤,任务可以取消
        public void Update()
        {
            List<DomainsEntity> domainsList = new List<DomainsEntity>();
            if (!File.Exists(StrWhere)) return;
            FileStream fs = new FileStream(StrWhere, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            if (fs == null) throw new IOException("Unable to open the file: " + StrWhere);
            StreamReader sr = new StreamReader(fs, Encoding.Default);
            string strTemp = "";
            string[] strs = null;


            IDataService<DomainsEntity> DomainsSv = ServiceBuilder.BuildDomainsService();
            QueryParam qp = new QueryParam();
            List<DomainsEntity> siteList = new List<DomainsEntity>();
            while ((strTemp = sr.ReadLine()) != null)
            {

                strs = strTemp.Split(',');
                if (strs.Length == 3)
                {
                    strs[0] = strs[0].ToLower();
                    if (strs[0].Length<=18 &&( strs[0].IndexOf(".com") > -1 || strs[0].IndexOf(".net") > -1 || strs[0].IndexOf(".org") > -1))
                    {
                        DomainsEntity domain = new DomainsEntity();
                        if (strs[0].IndexOf("http") == -1)
                        {
                            strs[0] = string.Concat("http://", strs[0]);
                        }

                        domain.SiteUrl = strs[0];
                        domain.DeleteTime = Utils.StringToDataTime(strs[1]);
                        domainsList.Add(domain);
                    }
                }
            }
            sr.Close();
            sr.Dispose();
            fs.Close();
            fs.Dispose();
            bool is_cancel = false;
            this.m_action = domainsList.Count;
            for (int i = 0; i < domainsList.Count; i++)
            {
                is_cancel = isCnacel();
                if (is_cancel)
                    break;
                int recordNum = 0;

                qp.Where = " siteUrl='" + domainsList[i].SiteUrl + "'";
                siteList = DomainsSv.List(qp, out recordNum);
                if (siteList.Count > 0)
                {


                }
                else
                {
                    DomainsSv.Insert(domainsList[i]);
                }
                OnTaskProgress(this, i);
                System.Threading.Thread.Sleep(10);
            }
            if (!is_cancel)
                OnTaskDone(this, null);
            this.Stop();

        }
    

    }
}
